﻿using Codemonk.Common.Logging;
using System;

namespace Codemonk.CloudBox.Unity.Logging
{
    public sealed class ConsoleLog : Log
    {
        public ConsoleLog(string path="")
        {
            System.Diagnostics.Debug.Write(path);
        }

        public System.Net.IPEndPoint Server { get; set; }

        public override void Record(LogLevel level, object message)
        {
            Console.WriteLine("{0:yyyy/MM/dd HH:mm:ss}-{1,-6}-{2}", DateTime.Now, level, message);
        }

        public override void Record(LogLevel level, object message, Exception exception)
        {
            Console.WriteLine("{0:yyyy/MM/dd HH:mm:ss}-{1,-6}-{2}-{3}", DateTime.Now, level, message, exception);
        }

        public override void Record(LogLevel level, object message, string format)
        {
            Console.WriteLine("{0:yyyy/MM/dd HH:mm:ss}-{1,-6}-{2}", DateTime.Now, level, string.Format(format, message));
        }

        public override void Record(LogLevel level, object message, string format, IFormatProvider provider)
        {
            if (provider == null)
                Console.WriteLine("{0:yyyy/MM/dd HH:mm:ss}-{1,-6}-{2}", DateTime.Now, level, string.Format(format, message));
            else
                Console.WriteLine("{0:yyyy/MM/dd HH:mm:ss}-{1,-6}-{2}", DateTime.Now, level, string.Format(format, provider, message));
        }
    }

    public sealed class ConsoleLogFactory : ILogFactory
    {
        private static ILog log;

        public ILog Get(string name = "default")
        {
            return log == null ? (log = new ConsoleLog()) : log;
        }
    }
}
